package javaer.starter.idea.util;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;

import java.io.IOException;

/**
 * @author yangzhen
 * @date 2023/8/9 13:55
 * @since 1.0.0
 */
public class IdeaFileUtils {

    final static LocalFileSystem localFileSystem = LocalFileSystem.getInstance();

    public static VirtualFile openFileEditor(Project project, String[] directoryNames, String filename, String content) throws IOException {
        VirtualFile virtualFile = createVirtualFile(project, directoryNames, filename, content);
        OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(project, virtualFile);
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        fileEditorManager.openTextEditor(openFileDescriptor, true);
        return virtualFile;
    }

    /**
     * 在项目下创建子目录
     *
     * @param project
     * @param directoryNames
     * @return
     * @throws IOException
     */
    public static VirtualFile createDirectoryVirtualFile(Project project, String[] directoryNames) throws IOException {
        return createDirectoryVirtualFile(project, project.getBaseDir(), directoryNames);
    }

    public static VirtualFile createDirectoryVirtualFile(Project project, VirtualFile baseDir, String[] directoryNames) throws IOException {
        VirtualFile storeVf = baseDir;
        VirtualFile tmpVf = null;
        for (String directoryName : directoryNames) {
            tmpVf = storeVf.findChild(directoryName);
            if (tmpVf == null) {
                tmpVf = storeVf.createChildDirectory(project, directoryName);
            }
            storeVf = tmpVf;
        }
        localFileSystem.refreshAndFindFileByNioFile(storeVf.toNioPath());
        return storeVf;
    }

    public static VirtualFile createVirtualFile(Project project, String[] directoryNames, String filename, String content) throws IOException {

        VirtualFile parentVf = createDirectoryVirtualFile(project, directoryNames);
        VirtualFile vf = createVirtualFile(project, parentVf, filename, content);

        localFileSystem.refreshAndFindFileByNioFile(vf.toNioPath());

        Document document = FileDocumentManager.getInstance().getDocument(vf);
        if (document != null) {
            document.setText(content);
        }
        return vf;
    }

    /**
     * 在指定目录下创建文件
     *
     * @param project
     * @param parentVf
     * @param filename
     * @param content
     * @return
     * @throws IOException
     */
    public static VirtualFile createVirtualFile(Project project, VirtualFile parentVf, String filename, String content) throws IOException {

        VirtualFile vf = parentVf.findOrCreateChildData(project, filename);
        localFileSystem.refreshAndFindFileByNioFile(vf.toNioPath());

        Document document = FileDocumentManager.getInstance().getDocument(vf);
        if (document != null) {
            document.setText(content);
        }
        return vf;
    }
}
